var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');


var YjxOpenapiAuthModel = function (obj) {
    YjxBaseModel.call(this,obj);

//    `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户id',
//        `key` varchar(50) DEFAULT NULL COMMENT '18位',
//        `secret` varchar(50) DEFAULT NULL COMMENT '32位',
//        `auth` int(2) NOT NULL DEFAULT '0' COMMENT '权限: 0无权限, 1有权限',
//        `tmpl_set_ids` varchar(500) DEFAULT NULL COMMENT '模版组id数组: [1,2]',
//        `user_id` bigint(20) DEFAULT NULL COMMENT '用户id(保留字段)',
//        `desc` varchar(500) DEFAULT NULL COMMENT '备注',
//        `create_time` datetime NOT NULL COMMENT '创建时间',
//        `update_time` datetime NOT NULL COMMENT '更新时间',
//        `update_by` varchar(50) DEFAULT NULL COMMENT '更新者',
//        `del_flg` int(11) NOT NULL DEFAULT '0' COMMENT '逻辑删除标志位',
//        PRIMARY KEY (`id`)

};

YjxOpenapiAuthModel.prototype = new YjxBaseModel();

// 取得表名
YjxOpenapiAuthModel.prototype.getTableName = function(){
    return 'yjx_openapi_auth';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxOpenapiAuthModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'id='+this.id;
};


/**
 * 查询key和secret
 *
 */
YjxOpenapiAuthModel.prototype.findKeySecretToDb = function(apiKey, callback){
    pool.pool.getConnection(function(connectErr, connection) {
        if (connectErr) {
            console.log('YjxOpenapiAuthModel.findKeySecretToDb Err:'+connectErr);
            return callback(connectErr, null);
        }
        var sql ="	SELECT	" +
                "	  yoa.key AS apiKey,	" +
                "	  yoa.secret AS apiSecret,	" +
                "	  yoa.tmpl_set_ids AS setIds,	" +
                "	  yoa.user_id AS userId,	" +
                "	  yoa.auth,	" +
                "	  DATE_FORMAT(	" +
                "	      yoa.create_time,	" +
                "	      '%Y-%m-%d %H:%i:%s'	" +
                "	    )  AS createTime,	" +
                "	  yoa.desc	" +
                "	FROM	" +
                "	  yjx.yjx_openapi_auth yoa " +
                "	WHERE yoa.key = '"+apiKey+"'	" +
                "	  AND yoa.del_flg = 0	" ;
        console.log(sql);
        connection.query(sql, function (error, result) {
            if (error) {
                console.log('[YjxOpenapiAuthModel.js.js] [findKeySecretToDb] find Error: ' + error);
            }
            connection.release();
            callback(error, result);
        });
    });
};



module.exports = YjxOpenapiAuthModel;

